package com.qf.fmcommodity.user.service.impl;

import com.qf.fmcommodity.user.entity.Users;
import com.qf.fmcommodity.user.mapper.UsersMapper;
import com.qf.fmcommodity.user.service.IUsersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.fmcommodity.user.utils.SaltUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * 用户  服务实现类
 * </p>
 *
 * @author 苇名一心
 * @since 2023-02-13
 */
@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements IUsersService {

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void regist(Users users) {

        //生成随机盐
        String salt = SaltUtils.getSalt(6);

        //保存到users对象中
        users.setSalt(salt);

        //加密的密码
        String password = users.getPassword();

        Md5Hash md5Hash = new Md5Hash(password, salt, 1024);

        String s = md5Hash.toHex();
        users.setPassword(s);

        users.setUserRegtime(new Date());
        users.setUserModtime(new Date());
        save(users);


    }
}
